/*
============================================================================
Name        : BlockSetFont.cpp
Author      : 
Version     :
Copyright   : 
Description : CBlockSetFont implementation
============================================================================
*/

#include "BlockSetFont.h"
#include "BlockSetNetWork.h"
#include "BlockSetAP.h"
#include "BlockSetFirstPage.h"
#include "eStockappui.h"
#include "Global.h"
#include <eStock_L2hy.rsg>
#include "Logger.h"

CBlockSetFont::CBlockSetFont(CBaseBlock* bsBack):
CBlock(bsBack),
m_nFont(m_pGlobal->m_nFont),
m_nSpaceUp(m_pGlobal->m_nSpaceUp),
m_nSpaceDown(m_pGlobal->m_nSpaceDown),
m_nSize(2),
m_nCurrent(0)
{
}

CBlockSetFont::~CBlockSetFont()
{
	for(TInt i=0; i<4; i++)
		delete m_Ctrls[i];
}

CBlockSetFont* CBlockSetFont::NewLC(CBaseBlock* bsBack)
{
	CBlockSetFont* self = new (ELeave)CBlockSetFont(bsBack);
	CleanupStack::PushL(self);
	self->ConstructL();
	return self;
}

CBlockSetFont* CBlockSetFont::NewL(CBaseBlock* bsBack)
{
	CBlockSetFont* self=CBlockSetFont::NewLC(bsBack);
	CleanupStack::Pop(); // self;
	return self;
}

void CBlockSetFont::ConstructL()
{
	CBlock::ConstructL();

	CGlobal::GetDesFromID(m_strTitle,LOC_BUF_MENU_SETTING);
	CGlobal::GetDesFromID(m_strOK,LOC_BUF_MENU_SAVE);
	TBuf<10> temp;
	CGlobal::GetDesFromID(temp,LOC_BUF_STR_FONTSIZE);	

#ifdef __SERIES60_3X__
	if(m_pGlobal->m_osMajor == 5)
		m_Slider = CSlider::NewL(temp,m_pGlobal->m_nFont,18,12);
	else
		m_Slider = CSlider::NewL(temp,m_pGlobal->m_nFont,16,10);
	m_Ctrls[0] = m_Slider;
#else
	m_selBox = CSelectBox::NewL();
	m_selBox->AddItem(0,LOC_BUF_STR_LFONT);
	m_selBox->AddItem(1,LOC_BUF_STR_MFONT);
	m_selBox->AddItem(2,LOC_BUF_STR_SFONT);
	m_selBox->SetCurIndex(1);
	m_Ctrls[0] = m_selBox;
#endif
	CGlobal::GetDesFromID(temp,LOC_BUF_STR_ANTIALIASED);	
	m_Checkbox = CCheckBox::NewL(temp);
	m_Checkbox->m_bLock = CGlobal::Static()->g_nAntialiased>0?ETrue:EFalse;
	m_Ctrls[1] = m_Checkbox;

	CGlobal::GetDesFromID(temp,LOC_BUF_STR_SPACEUP);	
	m_inputSpaceUp =  CSingleLineTextInput::NewL(temp, 2);
	m_inputSpaceUp->SetRange(0,10);
	temp.Zero();
	temp.AppendNum(m_pGlobal->m_nSpaceUp);
	m_inputSpaceUp->SetText(temp);
	m_Ctrls[2] = m_inputSpaceUp;


	CGlobal::GetDesFromID(temp,LOC_BUF_STR_SPACEDOWN);
	m_inputSpaceDown =  CSingleLineTextInput::NewL(temp, 2);
	m_inputSpaceDown->SetRange(0,10);
	temp.Zero();
	temp.AppendNum(m_pGlobal->m_nSpaceDown);
	m_inputSpaceDown->SetText(temp);
	m_Ctrls[3] = m_inputSpaceDown;

	if (CGlobal::Static()->m_osMajor == 3)
		m_Ctrls[0]->SetFocus(ETrue);
}

void CBlockSetFont::AddTabs()
{
	TBuf<8> label;
	CGlobal::GetDesFromID(label,LOC_BUF_STR_APSET);
	iTabbar->AppendTab(label, KTabSettingAp);
	CGlobal::GetDesFromID(label,LOC_BUF_MENU_TYPEFACE);
	iTabbar->AppendTab(label, KTabSettingFont);
	CGlobal::GetDesFromID(label,LOC_BUF_STR_FRESHSET);
	iTabbar->AppendTab(label, KTabSettingFresh);
    /*CGlobal::GetDesFromID(label,LOC_BUF_STR_FIRSTPAGESET);
    iTabbar->AppendTab(label, KTabSettingFirstPage);*/
}

void CBlockSetFont::HandleTabbarCommand(TInt aCommand)
{
	CBlock* block = NULL;
	switch (aCommand)
	{
	case KTabSettingFont:
		block = CBlockSetFont::NewL(m_pBlockBack);
		break;
	case KTabSettingAp:
		block = CBlockSetAP::NewL(m_pBlockBack);
		break;
	case KTabSettingFresh:
		block = CBlockSetNetWork::NewL(m_pBlockBack);
		break;
    case KTabSettingFirstPage:
        block = CBlockSetFirstPage::NewL(m_pBlockBack);
        break;
	default:
		break;
	}
	if (block)
		GetContainer()->AddBlock(block);
}

void CBlockSetFont::BeforeDraw(CFbsBitGc& g) const
{
	iTitlebar->SetTitleText(m_strTitle);
	CBlock::BeforeDraw(g);
}

void CBlockSetFont::SizeChanged()
{
	CBlock::SizeChanged();
	iToolbar->SetSoftKey(CToolbar::EOK, CToolbar::EBack);
	iTabbar->SetSelectedId(KTabSettingFont);
	for (int i = 0; i<4; i++)
	{
		TInt y = m_rect.iTl.iY + 5 + i*(m_nLineHeight+4);
		m_Ctrls[i]->SetRect(0, y, m_nScreenWidth, y+m_nLineHeight);
	}
}

void CBlockSetFont::OnInput(const TDesC8& strInput)
{
	if (m_kb->m_bInputGoodCode)
	{
		CBlock::OnInput(strInput);
		return;
	}
	
	TBuf<32> buf;
	buf.Copy(strInput);
	if (m_inputSpaceUp->Focus())
		m_inputSpaceUp->SetText(buf);
	else if (m_inputSpaceDown->Focus())
		m_inputSpaceDown->SetText(buf);
	DrawNow();
}

void CBlockSetFont::OnDraw(CFbsBitGc& g)const
{
	for(TInt i=0; i<4; i++)
		m_Ctrls[i]->Draw(g);
}

void CBlockSetFont::OnKeyEvent(const TInt& keyCode)
{
	if (keyCode==m_pGlobal->g_nKeyOK || keyCode==EStdKeyNkpAsterisk || keyCode == '*')
	{
		if(eStockUI->GetCurView() && eStockUI->GetCurView()->iContainer)
				eStockUI->GetCurView()->iContainer->DelBlock();
		return;
	}
	if (keyCode==m_pGlobal->g_nKeyCancel)
	{
		if (m_pGlobal->m_nFont!=m_nFont)
		{
			m_pGlobal->m_nFont = m_nFont;
			m_pGlobal->SetFont();
		}
		m_pGlobal->m_nSpaceUp = m_nSpaceUp;
		m_pGlobal->m_nSpaceDown = m_nSpaceDown;
		if(eStockUI->GetCurView() && eStockUI->GetCurView()->iContainer)
		{	
			eStockUI->GetCurView()->iContainer->SetRect(eStockUI->ApplicationRect());
			eStockUI->GetCurView()->iContainer->DelBlock();
		}
		return;
	}

	if (keyCode==m_pGlobal->g_nKeyUp || keyCode==m_pGlobal->g_nKeyDown)
	{
		TInt nOldCurrent = m_nCurrent;
		if (keyCode==m_pGlobal->g_nKeyUp)
		{
			m_nCurrent--;
			if (m_nCurrent<0)
			{
				m_nCurrent = 0;
				//iTabbar->SetFocus(ETrue);
				//DrawNow();
			}
		}
		else
		{
			m_nCurrent++;
			if (m_nCurrent>3)
			{
				m_nCurrent = 3;
				//iToolbar->SetFocus(ETrue);
				//DrawNow();
			}
		}
		if (nOldCurrent!=m_nCurrent)
		{
			m_Ctrls[nOldCurrent]->SetFocus(EFalse);
			m_Ctrls[m_nCurrent]->SetFocus(ETrue);
			DrawNow();
		}
		return;
	}
	if(m_Ctrls[m_nCurrent]->OnKeyDown(keyCode))
	{
		if (m_nCurrent==0)
		{
#ifdef __SERIES60_3X__
			m_pGlobal->m_nFont = m_Slider->GetValue();
#else
			m_pGlobal->m_nFont = m_selBox->GetCurValue();
#endif
			m_pGlobal->SetFont();
		}
		else if (m_nCurrent==1)
		{
			m_pGlobal->g_nAntialiased = m_Checkbox->m_bLock;
			m_pGlobal->SetFont();
		}
		else if (m_nCurrent==2)
		{
			m_pGlobal->m_nSpaceUp = m_inputSpaceUp->GetIntValue();
		}
		else
		{
			m_pGlobal->m_nSpaceDown = m_inputSpaceDown->GetIntValue();
		}
		if(eStockUI->GetCurView() && eStockUI->GetCurView()->iContainer)
			eStockUI->GetCurView()->iContainer->SetRect(eStockUI->ApplicationRect());
		DrawNow();
	}
}

#ifdef __SERIES60_3X__
void CBlockSetFont::OnMouseDown(const TPoint& aPt)
{
	TInt nPos = -1;
	for(TInt i=0; i<4; i++)
	{
		TInt top = m_Ctrls[i]->m_rect.iTl.iY;
		TInt bottom = m_Ctrls[i]->m_rect.iBr.iY;
		if (aPt.iY>top && aPt.iY<bottom)
		{
			nPos = i;
			break;
		}
	}
	if (nPos>=0)
	{
		m_Ctrls[m_nCurrent]->SetFocus(EFalse);
		m_nCurrent = nPos;
		m_Ctrls[m_nCurrent]->SetFocus(ETrue);
		if (m_Ctrls[m_nCurrent] == m_Slider)
		{
			if(m_Slider->OnMouseDown(aPt))
			{			
				m_pGlobal->m_nFont = m_Slider->GetValue();
				m_pGlobal->SetFont();
				if(eStockUI->GetCurView() && eStockUI->GetCurView()->iContainer)
					eStockUI->GetCurView()->iContainer->SetRect(eStockUI->ApplicationRect());
				DrawNow();
			}
		}
		DrawNow();
	}
}

void CBlockSetFont::OnMouseMove(const TPoint& aPt)
{
	TInt nPos = -1;
	for(TInt i=0; i<4; i++)
	{
		TInt top = m_Ctrls[i]->m_rect.iTl.iY;
		TInt bottom = m_Ctrls[i]->m_rect.iBr.iY;
		if (aPt.iY>top && aPt.iY<bottom)
		{
			nPos = i;
			break;
		}
	}
	if (nPos>=0)
	{
		if (m_nCurrent!=nPos)
		{
			m_Ctrls[m_nCurrent]->SetFocus(EFalse);
			m_nCurrent = nPos;
			m_Ctrls[m_nCurrent]->SetFocus(ETrue);
			DrawNow();
		}
		else if (m_Ctrls[m_nCurrent] == m_Slider)
		{
			if(m_Slider->OnMouseMove(aPt))
			{			
				m_pGlobal->m_nFont = m_Slider->GetValue();
				m_pGlobal->SetFont();
				if(eStockUI->GetCurView() && eStockUI->GetCurView()->iContainer)
					eStockUI->GetCurView()->iContainer->SetRect(eStockUI->ApplicationRect());
				DrawNow();
			}
		}
	}
}

void CBlockSetFont::OnMouseUp(const TPoint& aPt)
{
	TInt nPos = -1;
	for(TInt i=0; i<4; i++)
	{
		TInt top = m_Ctrls[i]->m_rect.iTl.iY;
		TInt bottom = m_Ctrls[i]->m_rect.iBr.iY;
		if (aPt.iY>top && aPt.iY<bottom)
		{
			nPos = i;
			break;
		}
	}
	if (nPos>=0)
	{
		m_Ctrls[m_nCurrent]->SetFocus(EFalse);
		m_nCurrent = nPos;
		m_Ctrls[m_nCurrent]->SetFocus(ETrue);
		if (nPos == 2 || nPos == 3)
			m_kb->SetInputType(CKeyBoard::NUMERIC);
		if (nPos == 1)
		{
			TInt n = m_nLineHeight/2;
			TInt nx = m_Checkbox->m_nXLock;
			TInt ny = m_Checkbox->m_nYLock;
			if (aPt.iX>=nx&&aPt.iX<=nx+n&&
				aPt.iY>=ny&&aPt.iY<=ny+n)
				OnKeyEvent(m_pGlobal->g_nKeySelect);
		}
		DrawNow();
	}
}
#endif   
